//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.aidong.dao.wills;

import com.aidong.dao.base.ConditionMap;
import com.aidong.dao.base.OrderSet;
import com.aidong.dao.base.Pagination;
import com.aidong.dao.wills.BaseDao;
import com.aidong.model.wills.ContractInfo;
import com.aidong.model.wills.ContractSign;
import com.aidong.model.wills.MemberContract;
import com.aidong.model.wills.MemberStopedCard;
import com.aidong.model.wills.PayList;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.springframework.stereotype.Repository;

@Repository
public class MemberStopedCardDao extends BaseDao<MemberStopedCard, Integer> {
	public MemberStopedCardDao() {
	}

	public List<MemberStopedCard> getListByMemberCode(String memberCode) {
		return this.queryList("memberCode=:p1", new Object[]{memberCode}, (new OrderSet()).desc("saleDate"), -1);
	}

	public void queryPageList(Pagination<ContractInfo> pg, ConditionMap cd) {
		ArrayList params = new ArrayList();
		String sqlw = cd.toSql(params);
		String sql = "select c.saleId, c.shopCode, c.saleDate, c.contractCode, c.memberCode, i.chName memberName, c.cardCode\n, c.status, c.addFee, c.payAmount, c.beginTime, c.endTime, c.cardType\n, c.beginStopTime, c.endStopTime, c.openDate,c.newContractCode, c.salesCode, c.salesCode2\nfrom MemberStopedCard c inner join MemberInfo i on c.memberCode=i.memberCode\n" + (sqlw.length() > 0?"where " + sqlw:"") + " order by c.saleDate desc";
		this.queryPageListBySql(ContractInfo.class, pg, sql, params.toArray());
	}

	public MemberStopedCard getStopedByMemberCode(String memberCode) {
		String sql = "select s.* from MemberContract c inner join ContractMember m on c.contractCode=m.contractCode\ninner join MemberStopedCard s on c.contractCode=s.contractCode\nwhere m.memberCode=:p1 and c.effectiveType=\'6\' and s.status=1 order by endStopTime";
		List list = this.queryListBySql(sql, new Object[]{memberCode});
		return list.size() > 0?(MemberStopedCard)list.get(0):null;
	}

	public void addStopedCard(MemberStopedCard stopedCard, List<PayList> payList) {
		this.add(stopedCard);
		Iterator sql = payList.iterator();

		while(sql.hasNext()) {
			PayList memberName = (PayList)sql.next();
			memberName.setSaleId(stopedCard.getSaleId());
			memberName.setContractCode(stopedCard.getContractCode());
			this.getSession().save(memberName);
			if("S011".equalsIgnoreCase(memberName.getPayType())) {
				String sign = "update PayAli set status=:p1, payListId=:p2, salesCode=:p3, salesCode2=:p4, updateTime=:p5 where barCode=:p6";
				this.updateBySql(sign, new Object[]{Integer.valueOf(2), memberName.getAutoId(), memberName.getSalesCode(), memberName.getSalesCode2(), new Date(), memberName.getNote()});
			}
		}

		String sql1 = "update MemberContract set effectiveDate=:p1, effectiveType=:p2, outAmount=:p3, updateTime=:p4 where contractCode=:p5";
		this.updateBySql(sql1, new Object[]{stopedCard.getBeginStopTime(), "6", stopedCard.getOutAmount(), new Date(), stopedCard.getContractCode()});
		if(stopedCard.getEarnestId() != null) {
			sql1 = "update MemberEarnest set status=1, useSaleId=:p1, useDate=getdate() where saleId=:p2";
			this.updateBySql(sql1, new Object[]{stopedCard.getSaleId(), stopedCard.getEarnestId()});
		}

		String memberName1 = (String)this.getBySql(String.class, "select chName from MemberInfo where memberCode=:p1", new Object[]{stopedCard.getMemberCode()});
		ContractSign sign1 = new ContractSign();
		sign1.setShopCode(stopedCard.getShopCode());
		sign1.setContractCode(stopedCard.getContractCode());
		sign1.setContractType("04");
		sign1.setSaleId(stopedCard.getSaleId());
		sign1.setMemberCode(stopedCard.getMemberCode());
		sign1.setMemberName(memberName1 == null?"":memberName1);
		sign1.setStatus(Integer.valueOf(1));
		sign1.setSalesCode(stopedCard.getSalesCode());
		sign1.setSalesCode2(stopedCard.getSalesCode2());
		sign1.setCreateUser(stopedCard.getCreateUser());
		sign1.setCreateTime(new Date());
		sign1.setUpdateTime(new Date());
		this.getSession().save(sign1);
	}

	public void addOpenCard(MemberStopedCard stopedCard, MemberContract contract) {
		String shopCode = contract.getShopCode();
		String sql = "select max(contractCode) from MemberContract where shopCode=:p1 and contractCode like :p2";
		String maxCode = (String)this.getBySql(String.class, sql, new Object[]{shopCode, shopCode + "%"});
		int maxId = 0;
		if(maxCode != null && maxCode.length() > shopCode.length()) {
			try {
				maxId = Integer.parseInt(maxCode.substring(shopCode.length()));
			} catch (Exception var8) {
				;
			}
		}

		String contractCode = "000000" + (maxId + 1);
		contractCode = shopCode + contractCode.substring(contractCode.length() - 6);
		stopedCard.setNewContractCode(contractCode);
		sql = "update MemberStopedCard set status=4, newContractCode=:p1, openDate=:p2, openUser=:p3, updateTime=getdate() where saleId=:p4";
		this.updateBySql(sql, new Object[]{stopedCard.getNewContractCode(), stopedCard.getOpenDate(), stopedCard.getOpenUser(), stopedCard.getSaleId()});
		contract.setContractCode(contractCode);
		this.getSession().save(contract);
		sql = "insert into ContractMember (ContractCode, MemberCode)\nselect \'" + contractCode + "\',memberCode from ContractMember where contractCode=:p1";
		this.updateBySql(sql, new Object[]{stopedCard.getContractCode()});
		sql = "insert into ContractShop (ContractCode, ShopCode)\nselect \'" + contractCode + "\',shopCode from ContractShop where contractCode=:p1";
		this.updateBySql(sql, new Object[]{stopedCard.getContractCode()});
	}
}
